;(function() {
	Nokia.WeatherView = Nokia.View.extend({
		
		/*
		 * Sets the weather result items
		 */
		setItems: function(items)
		{
			this.weatherItems = items;
		},
		
		/*
		 * Sets the available dates for the weather forecasts
		 */
		setDates: function(datesArray)
		{
			this.weatherDates = datesArray;
			
			// Create the array of items for the weather DropDown component
			
			var dateItems = new Array();
			
			var instance = this;
			
			for(var i = 0; i < datesArray.length; i++)
			{
				dateItems.push({
					label: Nokia.GBCUtils.formatLongDate(datesArray[i]),
					value: datesArray[i],
					dateIndex: i,
					select: function(item, event) {
						instance.showDate(item.dateIndex);
					}
				});
			}
			
			// Create the HTML code for the dates DropDown component
			
			$('#weather-forecasts-container').html(
				'<div id="weather-forecasts-dropdown"></div>' + 
				'<div id="weather-forecasts"></div>'
			);
			
			// Create the Guarana DropDown component
			
			this.weatherFilter = new Nokia.DropDown({
				element: '#weather-forecasts-dropdown',
				
				items: dateItems
			});
		},
		
		/*
		 * Shows the weather forecasts for a specific date
		 */
		showDate: function(dateIndex)
		{
			this.weatherFilter.select(dateIndex);
			
			var dateDay = Nokia.GBCUtils.formatDate(this.weatherDates[dateIndex]);
			
			var weatherHTML = '';
			
			var prevTime = null;
			
			var forecasts = [];

			for(var i = 0; i < this.weatherItems.length; i++)
			{
				var item = this.weatherItems[i];
				
				if(item.day == dateDay && item.symbol_number)
				{
					var itemTime =  Nokia.GBCUtils.formatTime(item.from);
					
					forecasts[itemTime] = item;
				}
			}
			var forecastTimes = ['03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'];
			
			for(var i = 0; i < forecastTimes.length; i++)
			{
				var item = forecasts[forecastTimes[i]]
				
				if(item)
				{
					weatherHTML += 
						'<div class="weather-subitem ui-helper-clearfix">' + 
						'<div class="weather-subitem-icon"><img src="http://api.yr.no/weatherapi/weathericon/1.0/?symbol=' + item.symbol_number + ';content_type=image/png" /></div>' + 
						'<div class="weather-subitem-time"> at ' + forecastTimes[i] + '</div>' + 
						'</div>';
				}
			}
			
			$('#weather-forecasts').html(weatherHTML);
		},

		/*
		 * Shows the view and the forecasts for the given date index
		 */		
		show: function(dateIndex)
		{
			// Replace the Exit button with a Back button
			
			var template = this.getTemplate();
			var topBar = template.getHeaderTopBar();
			var viewManager = template.getViewManager();
			
			var defaultTopItems = GBC.getDefaultTopItems();
			
			defaultTopItems[0] = {classname: 'nokia-template-header-icon-back', callback: function() { viewManager.back() }};
			
			topBar.setItems(defaultTopItems);
			
			this.showDate(dateIndex);
			
			this.getContainer().show();
		}
	});
})();